﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace mki.Backup.Objects.Controls
{
	public partial class mkiTokenTextBox : TextBox
	{
		#region Fields (1)

		// ReadOnly Fields (1) 

		private readonly ContextMenu tokenContextMenu = new ContextMenu();

		#endregion Fields

		#region Constructors (2)

		/// <summary>
		/// Initializes a new instance of the <see cref="mkiTokenTextBox"/> class.
		/// </summary>
		/// <param name="container">The container.</param>
		public mkiTokenTextBox(IContainer container)
		{
			container.Add(this);

			InitializeComponent();

			// init
			init();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="mkiTokenTextBox"/> class.
		/// </summary>
		public mkiTokenTextBox()
		{
			InitializeComponent();

			// init
			init();
		}

		#endregion Constructors

		#region Properties (1)

		// Public Properties (1) 

		[Description("Enables TokenContextMenu for this TextBox"),
		 Category("Behavior"),
		 DefaultValue(true),
		 Browsable(true)]
		public Boolean TokenContextMenu
		{
			get { return this.ContextMenu == this.tokenContextMenu; }
			set
			{
				this.ContextMenu = null;
				if (value)
				{
					this.ContextMenu = this.tokenContextMenu;
				}
			}
		}

		#endregion Properties

		#region Methods (2)

		// Private Methods (2) 

		/// <summary>
		/// Inits this instance.
		/// </summary>
		private void init()
		{
			this.tokenContextMenu.MenuItems.Add(new MenuItem("{ServerName}", tokenContextMenuClick));
			this.tokenContextMenu.MenuItems.Add(new MenuItem("{Date}", tokenContextMenuClick));
			this.tokenContextMenu.MenuItems.Add(new MenuItem("{Time}", tokenContextMenuClick));
			this.ContextMenu = this.tokenContextMenu;
		}

		/// <summary>
		/// Tokens the context menu click.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void tokenContextMenuClick(object sender, EventArgs e)
		{
			var menuItem = sender as MenuItem;
			if (menuItem != null)
			{
				this.Paste(menuItem.Text);
			}
		}

		#endregion Methods
	}
}